/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.importing.email;

import org.processmining.mining.*;
import org.processmining.email.*;
import org.processmining.framework.log.*;
import org.processmining.framework.plugin.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;

/**
 * <p>An extension of MiningResult class to show the imported e-mail log. </p>
 *
 * @author Andriy Nikolov
 * @version 1.0
 */
public class EMailMiningResult extends JPanel implements MiningResult, Provider {
  EMailLog eMailLog;
  EMailLogTableModel eMailLogTableModel;
  EmployeesListTableModel employeesListTableModel;
  ListSelectionModel employeesListSelectionModel;
  JTabbedPane jTabbedPaneSNA = new JTabbedPane();
  JPanel jPanelMessageList = new JPanel();
  JPanel jPanelEmployees = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPaneMessageList = new JScrollPane();
  JTable jTableMessageList = new JTable();
  JSplitPane jSplitPane1 = new JSplitPane();
  BorderLayout borderLayout2 = new BorderLayout();
  JPanel jPanelEmployeesList = new JPanel();
  JPanel jPanelAliases = new JPanel();
  JPanel jPanelEmployeesListHeader = new JPanel();
  BorderLayout borderLayout3 = new BorderLayout();
  JPanel jPanelEmployeesListFooter = new JPanel();
  JScrollPane jScrollPaneEmployeesList = new JScrollPane();
  JTable jTableEmployeesList = new JTable();
  JPanel jPanelAliasesHeader = new JPanel();
  BorderLayout borderLayout4 = new BorderLayout();
  JPanel jPanelAliasesFooter = new JPanel();
  JScrollPane jScrollPaneAliases = new JScrollPane();
  JList jListAliases = new JList();
  JLabel jLabelUsers = new JLabel();
  JLabel jLabelAliases = new JLabel();
  JButton jButtonMerge = new JButton();
  JTextField jTextFieldNewAlias = new JTextField();
  BorderLayout borderLayout5 = new BorderLayout();
  JButton jButtonAddAlias = new JButton();
  JButton jButtonRemoveAlias = new JButton();
  JPanel jPanelButtons = new JPanel();
  JButton jButtonMainName = new JButton();
  BorderLayout borderLayout6 = new BorderLayout();
  BorderLayout borderLayout7 = new BorderLayout();
  BorderLayout borderLayout8 = new BorderLayout();
  BorderLayout borderLayout9 = new BorderLayout();
  BorderLayout borderLayout10 = new BorderLayout();
  BorderLayout borderLayout11 = new BorderLayout();
  BorderLayout borderLayout12 = new BorderLayout();
  BorderLayout borderLayout13 = new BorderLayout();
  BorderLayout borderLayout14 = new BorderLayout();
  int currentEmployee;
  DefaultListModel aliasesListModel = new DefaultListModel();

  public EMailMiningResult(EMailLog log) {
    eMailLog = log;
    try {
      jbInit();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public LogReader getLogReader() {
    return null;
  }

  public JComponent getVisualization() {
    return this;
  }

  public ProvidedObject[] getProvidedObjects()
  {
    return (new ProvidedObject[] {
      new ProvidedObject("E-mail log", new Object[] {
        eMailLog
      })
    });
  }

  public void jbInit() {
    this.setLayout(borderLayout9);
    jPanelMessageList.setLayout(borderLayout10);
    jPanelEmployees.setLayout(borderLayout11);
    jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
    jPanelEmployeesList.setLayout(borderLayout12);
    jPanelEmployeesListHeader.setBorder(BorderFactory.createEtchedBorder());
    jPanelEmployeesList.setBorder(BorderFactory.createEtchedBorder());
    jPanelEmployeesListFooter.setBorder(BorderFactory.createEtchedBorder());
    jLabelUsers.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jLabelUsers.setText("Employees");
    jTabbedPaneSNA.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonMerge.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonMerge.setText("Merge");
    jButtonMerge.addActionListener(new
                                   EMailMiningResult_jButtonMerge_actionAdapter(this));
    jPanelAliases.setLayout(borderLayout13);
    jPanelAliasesHeader.setBorder(BorderFactory.createEtchedBorder());
    jPanelAliases.setBorder(BorderFactory.createEtchedBorder());
    jPanelAliasesFooter.setBorder(BorderFactory.createEtchedBorder());
    jPanelAliasesFooter.setLayout(borderLayout14);
    jButtonMainName.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonMainName.setText("Main name");
    jButtonMainName.addActionListener(new
        EMailMiningResult_jButtonMainName_actionAdapter(this));
    jButtonAddAlias.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonAddAlias.setMaximumSize(new Dimension(57, 25));
    jButtonAddAlias.setMinimumSize(new Dimension(57, 25));
    jButtonAddAlias.setPreferredSize(new Dimension(57, 25));
    jButtonAddAlias.setText("Add");
    jButtonAddAlias.addActionListener(new
        EMailMiningResult_jButtonAddAlias_actionAdapter(this));
    jButtonRemoveAlias.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonRemoveAlias.setMaximumSize(new Dimension(79, 25));
    jButtonRemoveAlias.setMinimumSize(new Dimension(79, 25));
    jButtonRemoveAlias.setPreferredSize(new Dimension(79, 25));
    jButtonRemoveAlias.setText("Remove");
    jButtonRemoveAlias.addActionListener(new
        EMailMiningResult_jButtonRemoveAlias_actionAdapter(this));
    this.setMinimumSize(new Dimension(600, 110));
    jLabelAliases.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jLabelAliases.setText("Aliases");
    this.add(jTabbedPaneSNA, java.awt.BorderLayout.CENTER);
    jTabbedPaneSNA.add(jPanelMessageList, "Messages");
    jTabbedPaneSNA.add(jPanelEmployees, "Employees");
    jPanelMessageList.add(jScrollPaneMessageList, java.awt.BorderLayout.CENTER);
    jPanelEmployeesList.add(jPanelEmployeesListFooter,
                            java.awt.BorderLayout.SOUTH);
    jPanelEmployeesListFooter.add(jButtonMerge);
    jPanelEmployeesList.add(jPanelEmployeesListHeader,
                            java.awt.BorderLayout.NORTH);
    jPanelEmployeesListHeader.add(jLabelUsers);
    jPanelEmployeesList.add(jScrollPaneEmployeesList,
                            java.awt.BorderLayout.CENTER);
    jScrollPaneEmployeesList.getViewport().add(jTableEmployeesList);
    jScrollPaneMessageList.getViewport().add(jTableMessageList);
    jPanelEmployees.add(jSplitPane1, java.awt.BorderLayout.CENTER);
    jSplitPane1.add(jPanelEmployeesList, JSplitPane.LEFT);
    jSplitPane1.add(jPanelAliases, JSplitPane.RIGHT);
    jPanelAliases.add(jPanelAliasesFooter, java.awt.BorderLayout.SOUTH);
    jPanelAliases.add(jPanelAliasesHeader, java.awt.BorderLayout.NORTH);
    jPanelAliasesHeader.add(jLabelAliases);
    jPanelAliases.add(jScrollPaneAliases, java.awt.BorderLayout.CENTER);
    jScrollPaneAliases.getViewport().add(jListAliases);
    jPanelAliasesFooter.add(jPanelButtons, java.awt.BorderLayout.CENTER);
    jPanelButtons.add(jButtonMainName);
    jPanelButtons.add(jButtonAddAlias);
    jPanelButtons.add(jButtonRemoveAlias);
    jPanelAliasesFooter.add(jTextFieldNewAlias, java.awt.BorderLayout.NORTH);
    employeesListSelectionModel = jTableEmployeesList.getSelectionModel();
    employeesListSelectionModel.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        jTableEmployees_selectionValueChanged(e);
      }
    } );
    eMailLogTableModel = new EMailLogTableModel(eMailLog);
    jTableMessageList.setModel(eMailLogTableModel);
    jTableMessageList.getColumnModel().getColumn(0).setMaxWidth(25);
    jTableMessageList.getColumnModel().getColumn(4).setMaxWidth(70);
    jTableMessageList.getColumnModel().getColumn(5).setMaxWidth(50);

    employeesListTableModel = new EmployeesListTableModel(eMailLog);
    jTableEmployeesList.setModel(employeesListTableModel);
    jListAliases.setModel(aliasesListModel);
    jTableEmployeesList.getColumnModel().getColumn(1).setMaxWidth(50);
    jListAliases.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    currentEmployee = -1;
  }

  public void jButtonMerge_actionPerformed(ActionEvent e) {
    ListSelectionModel lsm;
    int minIndex, curIndex;
    int i;
    Vector selection = new Vector();
    EMailRecipient leader, tmp;
    lsm = (ListSelectionModel)jTableEmployeesList.getSelectionModel();
    if(!lsm.isSelectionEmpty()) {
      minIndex = lsm.getMinSelectionIndex();
      leader = (EMailRecipient)eMailLog.getEmployeesList().get(minIndex);
      for(i=0;i<jTableEmployeesList.getRowCount();i++) {
        if (lsm.isSelectedIndex(i)) {
          if(i!=minIndex) {
            tmp = (EMailRecipient) eMailLog.getEmployeesList().get(i);
            selection.add(tmp);
          }
        }
      }
      for(i=0;i<selection.size();i++) {
        tmp = (EMailRecipient)selection.get(i);
        eMailLog.mergeRecipients(leader, tmp);
      }
      jTableEmployeesList.revalidate();
      updateAliasesList();
    }
  }

  public void jButtonMainName_actionPerformed(ActionEvent e) {
    String name;
    EMailRecipient tmp;
    name = (String)aliasesListModel.get(jListAliases.getSelectedIndex());
    tmp = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    tmp.setName(name);
    jTableEmployeesList.revalidate();
  }

  public void jButtonAddAlias_actionPerformed(ActionEvent e) {
    String name;
    EMailRecipient tmp;

    name = jTextFieldNewAlias.getText();
    tmp = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    if(name!="")
      tmp.addAlias(name);
    jTextFieldNewAlias.setText("");
    updateAliasesList();
  }

  public void jButtonRemoveAlias_actionPerformed(ActionEvent e) {
    String name;
    EMailRecipient tmp;
    tmp = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    tmp.removeAlias(jListAliases.getSelectedIndex());
    updateAliasesList();
  }

  public void jTableEmployees_selectionValueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm;
    lsm = (ListSelectionModel)e.getSource();
    if(!lsm.isSelectionEmpty()) {
      currentEmployee = lsm.getMinSelectionIndex();
      updateAliasesList();
    }
  }

  public void updateAliasesList() {
    EMailRecipient eMailRecipient;
    String curAlias;
    aliasesListModel.clear();
    eMailRecipient = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    for (int i=0;i<eMailRecipient.getAliases().size();i++) {
      aliasesListModel.addElement(eMailRecipient.getAlias(i));
    }
  }

  public void setEMailLog(EMailLog log) {
    eMailLog = log;
  }

  public EMailLog getEMailLog() {
    return eMailLog;
  }
}

class EMailMiningResult_jButtonRemoveAlias_actionAdapter
    implements ActionListener {
  private EMailMiningResult adaptee;
  EMailMiningResult_jButtonRemoveAlias_actionAdapter(EMailMiningResult adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButtonRemoveAlias_actionPerformed(e);
  }
}

class EMailMiningResult_jButtonAddAlias_actionAdapter
    implements ActionListener {
  private EMailMiningResult adaptee;
  EMailMiningResult_jButtonAddAlias_actionAdapter(EMailMiningResult adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButtonAddAlias_actionPerformed(e);
  }
}

class EMailMiningResult_jButtonMainName_actionAdapter
    implements ActionListener {
  private EMailMiningResult adaptee;
  EMailMiningResult_jButtonMainName_actionAdapter(EMailMiningResult adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButtonMainName_actionPerformed(e);
  }
}

class EMailMiningResult_jButtonMerge_actionAdapter
    implements ActionListener {
  private EMailMiningResult adaptee;
  EMailMiningResult_jButtonMerge_actionAdapter(EMailMiningResult adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButtonMerge_actionPerformed(e);
  }
}
